<?php
include('HTGPage.class.php');


$page = new HTGPage();
/* @var $utils Utils */
$utils = $page->myUtils;

/*@var $captcha HTGCaptcha */
$captcha = $page->myCaptcha;

$errorArray = array();

$displayForgotForm=true;
$displayEmailSent=false;
$displayResetPasswordForm=false;
$displayCompletedForm=false;

if(!empty($_REQUEST['id'])) {
	$displayForgotForm=false;
}


if(!(empty($_REQUEST['reset_email']))) {  //If they posted the form here
	$email = $_REQUEST['reset_email'];
	if($utils->validEmail($email)) {

		//verify CAPTCHA stuff if we're on a host that has enius.com
		if(stristr($_SERVER['HTTP_HOST'], 'enius.com')) {
			if(!($captcha->validateCaptcha())) {
				$displayForgotForm=true;
				array_push($errorArray, "The two words you typed into our spam filter didn\'t seem to match what was displayed.  <br/>Please type the two funny-looking words in the space right below it to help us verify you\'re not a spammer.  <br />We know it's a pain, but it helps us focus on making better picks for you.");
			}
		}
		
		if(empty($errorArray)) {
			$users = $utils->getUser($email);
			if(count($users)) {
				$user = $users[0];
					
				//create a confirmation id
				$confirmId = md5(time());
				$utils->insertForgottenPassword($user['u_id'], $confirmId);
				$url = 'http://halftimegenius.com/reset.php?id=' . urlencode($confirmId);

				//send email with confirmation id
				$to = $user['u_email'];
				$subject = 'HalfTime Genius Password Reset Notification';
				$body = 'Thank you for using halftime genius.  To reset your password please go to the following URL:' . $url . '</a>';
				if(stristr($_SERVER['HTTP_HOST'], 'enius.com')) {
					include_once("Mail.php"); //Import this for GoDaddy hosting
				}
				mail($to, $subject, $body);
				$displayForgotForm=false;
				$displayEmailSent=true;
			}
		}
	}

}

if($displayForgotForm) {
	//Form to diesplay
	$page->preHeadCloseContent .= '<link rel="stylesheet" href="reset.css" type="text/css" />';

	$page->content .= '<div id="resetFormDiv"><br /><br /><strong style="font-size: 28px;">Reset your password</strong><form id="resetForm">';
	$page->content .= $utils->get_include_contents("resetForm.html");
	$page->content .= $captcha->getCaptchaForm();
	$page->preHeadCloseContent .= $captcha->getCaptchaOptions();

	$page->content .= 	'<fieldset name="submitFieldset">
			<input type="submit" name="submit" value="submit" />
		</fieldset>';
	$page->content .= '</form></div>';
}

if($displayEmailSent) {
	$page->content .= '<h1>An Email Has Been sent to you with a link that will reset your password.</h1>';	
}





	$page->display();
?>